/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * NeedReceiveRefundServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import com.emm.yixun.common.model.dto.SubscribeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jpush.api.utils.StringUtils;

import com.emm.yixun.basic.mapper.NeedReceiveRefundMapper;
import com.emm.yixun.basic.service.NeedReceiveRefundService;
import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.NeedReceiveRefundInfo;
import com.emm.yixun.common.model.dto.NeedReceiveRefundDto;
import com.emm.yixun.common.model.dto.NeedReceiveRefundInfoDto;
import com.emm.yixun.common.page.Page;


/**
 * [文件名称]<br>
 * NeedReceiveRefundServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 待收/退款表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-07-04 14:18:21 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("needReceiveRefundService")
public class NeedReceiveRefundServiceImpl implements NeedReceiveRefundService {
	
	@Autowired
	private NeedReceiveRefundMapper mapper;
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<NeedReceiveRefund> getPageModel(NeedReceiveRefundDto entity,int begin,int end){
		Integer total = mapper.count(entity);
		List<NeedReceiveRefund> rows = mapper.list(entity,begin,end);
	    Page<NeedReceiveRefund> msr = new Page<NeedReceiveRefund>(total, rows);
	    msr.setPageSize(end);
		return msr;
	}
	
	/**
	 * @param entity 搜索条件
	 * @param begin 分页开始行数[不包含]
	 * @param end 分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<NeedReceiveRefundInfo> getPageModelInfo(NeedReceiveRefundInfoDto entity,int begin,int end){
		Integer total = mapper.countInfo(entity);
		if(!StringUtils.isEmpty(entity.getIsExport())){
			begin = 0;
			end = total;
		}
		List<NeedReceiveRefundInfo> rows = mapper.listInfo(entity,begin,end);
	    Page<NeedReceiveRefundInfo> msr = new Page<NeedReceiveRefundInfo>(total, rows);
	    msr.setPageSize(end);
		return msr;
	}

	/**
	 * 查询列表
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public List<NeedReceiveRefund> selectListByDto(NeedReceiveRefundDto entity){
		return mapper.selectListByDto(entity);
	}
	
	/**
	 * 查询单个
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public NeedReceiveRefund selectByDto(NeedReceiveRefundDto entity){
		return mapper.selectByDto(entity);
	}
	
	/**
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public NeedReceiveRefund selectById(Long id){
		if(id!=null){
			return mapper.selectById(id);
		}
		return null;
	}
	
	/**
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public NeedReceiveRefundInfo selectInfoById(Long id){
		if(id!=null){
			return mapper.selectInfoById(id);
		}
		return null;
	}
	
	/**
	 * @param entity 新增记录的详细信息
	 * @return 新增行数
	 */
	public NeedReceiveRefundDto create(NeedReceiveRefundDto entity) {
		entity.setCreateTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity 更新记录的详细信息
	 * @return 更新行数
	 */
	public int update(NeedReceiveRefundDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		return mapper.edit(entity);
	}
    
	/**
	 * @param entity 待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(NeedReceiveRefundDto entity) {
		return mapper.del(entity);
	}
	
	/**
	 * @param entity 新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(NeedReceiveRefundDto entity) {
		if(entity!=null&&entity.getId()!=null){
			return update(entity);
		}else{
			create(entity);
			return 1;
		}
	}
	
	public List<NeedReceiveRefundDto> selectNrrfDtoListByDto(
			NeedReceiveRefundDto entity) {
		return mapper.selectNrrfDtoListByDto(entity);
	}
	
	/**
	 * 签约回款提醒条数
	 * @param entity 搜索条件
	 * @return 数量
	 */
	public int selectNonpaymentNum(NeedReceiveRefundDto entity, Long projectId, int day){
		return mapper.selectNonpaymentNum(entity, projectId, day);
	}
	
	/**
	 * 签约回款提醒分页
	 * @param entity 搜索条件
	 * @return 分页记录
	 */
	public Page<NeedReceiveRefundInfo> selectNonpaymentPage(NeedReceiveRefundDto entity, Long projectId, int day,
			int begin, int end){
		Integer total = mapper.selectNonpaymentNum(entity, projectId, day);
		List<NeedReceiveRefundInfo> rows = mapper.selectNonpaymentList(entity, projectId, day, begin, end);
	    Page<NeedReceiveRefundInfo> msr = new Page<NeedReceiveRefundInfo>(total, rows);
	    msr.setPageSize(end);
		return msr;
	}
	
	/**
	 * 签约回款提醒列表
	 * @param entity 搜索条件
	 * @return 列表
	 */
	public List<NeedReceiveRefundInfo> selectNonpaymentList(NeedReceiveRefundDto entity, Long projectId, int day){
		return mapper.selectNonpaymentList(entity, projectId, day, 0, 0);
	}

	/**
	 * 获取逾期未付款记录数
	 * @param entity
	 * @return
	 */
	public int getOverduePaymentListCount(NeedReceiveRefundDto entity) {
		return mapper.getOverduePaymentListCount(entity);
	}

	/**
	 * 获取逾期未付款记录
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return
	 */
	public Page<NeedReceiveRefundInfo> getOverduePaymentPageModel(NeedReceiveRefundDto entity, int begin, int end) {
		Integer total = mapper.getOverduePaymentListCount(entity);
		List<NeedReceiveRefundInfo> rows = mapper.getOverduePaymentList(entity, begin, end);
		Page<NeedReceiveRefundInfo> msr = new Page<NeedReceiveRefundInfo>(total, rows);
		msr.setPageNumber(begin);
		msr.setPageSize(end);
		return msr;
	}
}
